//
//  statusPictureView.swift
//  sinazd
//
//  Created by apple on 16/3/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
private let picturCellMargin: CGFloat = 5
private let picturCellId = "picturCellId"

class statusPictureView: UICollectionView {

   
    var imgUrls: [NSURL]?{
        didSet {
            //1.根据配图的数量 计算配图视图的大小
            let pSize = calculatePictureViewSize()
            //更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            //2.刷新集合视图
           testLabel.text = "\(imgUrls?.count)"
            reloadData()
        }

    }
    
    
    //重写构造方法
    init() {
        //必须调用指定的构造函数
        //布局对象 必须是  '流水'布局对象
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumInteritemSpacing = picturCellMargin
        layout.minimumLineSpacing = picturCellMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: picturCellId)
        
        
        //TODO:数据源代理
        self.dataSource = self
        
        self.backgroundColor = UIColor.whiteColor()
        
        self.scrollsToTop = false
        
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
   //TODO:测试用
    private func setupUI() {
        
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    //TODO:测试用
    private lazy var testLabel: UILabel = UIFactory.labelWith("", textColor: UIColor.redColor(), fontSize: 40)


    
    private func calculatePictureViewSize() -> CGSize {
        let imageCount = imgUrls?.count ?? 0
        let maxWidth = ScreenWidth - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * picturCellMargin) / 3
        
        //如何获取layout对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置itemSize
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            //给定图片远尺寸大小
            //TODO:  比较困难 后续完善 线给定 固定大小
            let imageSize = CGSizeMake(150, 100)
            //TODO: 需要设置 itmeSize
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            //计算配图大小  2 * itemSize.width + margin
            let w = itemWidth * 2 + picturCellMargin
            return CGSizeMake(w, w)
        }
        
        //其他多张图片
        //确定行数  根据图片数量确定有多少行
        /*
        1,2,3   -> 1
        4,5,6   -> 2
        7,8,9   -> 3
        */
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let pHeight = row * itemWidth + (row - 1) * picturCellMargin
        
        
        return CGSizeMake(maxWidth, pHeight)
    }
    
}

//数据源协议
extension statusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(picturCellId, forIndexPath: indexPath) as! PictureCell
        
        //        //设置测试颜色
        //        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        
        //绑定数据
        cell.imageURL = imgUrls![indexPath.item]
        return cell
    }
    
}

